//
//  DynamicCell.m
//  BigJoy
//
//  Created by Rowling on 2017/7/14.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "DynamicCell.h"

@interface DynamicCell()
@property (weak, nonatomic) IBOutlet UIView *imageContentView;
@property (weak, nonatomic) IBOutlet UILabel *signLable;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *headH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pinlunH;
@property (weak, nonatomic) IBOutlet UIView *pinglunContentView;
@property (weak, nonatomic) IBOutlet UIImageView *jiantouXiaImageView;

@end

@implementation DynamicCell

+(instancetype)cellWithTalbelView:(UITableView *)table {
    DynamicCell * cell = [table dequeueReusableCellWithIdentifier:@"dynamicCell"];
    if (cell == nil) {
        cell = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
    }
    return cell;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
}

-(void)setViewModel:(DynamicCellViewModel *)viewModel {
    _viewModel = viewModel;
    // 移除之前cell的子视图
    for (UIView * view  in self.imageContentView.subviews) {
        [view removeFromSuperview];
    }
    for (UIView * view  in self.pinglunContentView.subviews) {
        [view removeFromSuperview];
    }
    //布局签名高度
    self.signLable.text = viewModel.contentStr;
    self.headH.constant = viewModel.headHeight;
    // 布局image
    if (viewModel.imageArray.count == 0) {
        
    }else if (viewModel.imageArray.count == 1) {
        UIImageView * imageVIew = [[UIImageView alloc]initWithImage:[viewModel.imageArray firstObject]];
        imageVIew.frame = CGRectMake(0, 0, viewModel.singleW, viewModel.singleH);
        [self.imageContentView addSubview:imageVIew];
    }else if (viewModel.imageArray.count > 1) {
        NSInteger count = viewModel.imageArray.count > 9 ? 9 : viewModel.imageArray.count;
        for (int i =0; i < count; i ++) {
            int row = i / 3;
            int col = i % 3;
            UIImageView * imageView =  [[UIImageView alloc]initWithImage:[viewModel.imageArray firstObject]];
            imageView.frame = CGRectMake(col * (viewModel.moreWH + 10), row * (viewModel.moreWH + 10) , viewModel.moreWH, viewModel.moreWH);
            [self.imageContentView addSubview:imageView];
        }
    }
    // 布局评论
    self.pinlunH.constant = viewModel.pinlunH;
    self.jiantouXiaImageView.hidden = YES;
    if (viewModel.pinlunArray.count > 0) {
        self.jiantouXiaImageView.hidden = NO;
        NSInteger count = viewModel.pinlunArray.count > 2 ?  2 : viewModel.pinlunArray.count;
        for (int i = 0;i < count;i ++) {
            NSString * str = viewModel.pinlunArray[i];
            UILabel * lable = [XMSuperHelper XMLableWithColor:[UIColor colorForHex:@"333333"] Title:[NSString stringWithFormat:@"晓明哥:%@",str] FontSize:12];
            lable.frame = CGRectMake(0, 10 + i * 20, SCREEN_WIDTH - 30, 12);
            [self.pinglunContentView addSubview:lable];
        }
        // 查看更多评论按钮
        if (viewModel.pinlunArray.count > 2) {
            UIButton * btn = [XMSuperHelper buttonWithTitle:@">> 点击查看更多" Font:[UIFont systemFontOfSize:11] fontColor:[UIColor colorForHex:@"666666"]];
            [self.pinglunContentView addSubview:btn];
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.pinglunContentView).offset(-15);
                make.bottom.equalTo(self.pinglunContentView).offset(-10);
            }];
        }
        //下面的那根线条
        UIView * lineView = [[UIView alloc]init];
        [self.pinglunContentView addSubview:lineView];
        lineView.backgroundColor = XMColor(221, 221, 221);
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.equalTo(self.pinglunContentView);
            make.height.equalTo(@1);
        }];
    }
}

- (IBAction)buttonClick:(UIButton *)sender {
    NSInteger tag = sender.tag - 100;
    if (self.buttonClickBlock) {
        self.buttonClickBlock(tag);
    }
}


@end
